from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from logs.log import get_log

class Base():
    #利用显示等待封装了一个新的定位元素的方法find_element_neW
    #driver驱动
    #by定位元素的方法AppiumBy.ID,AppiumBy.XPATH
    #time_out默认10秒，用于显式等待的时间
    logger = get_log("自动化")
    def find_element_new(self,driver,by,value,time_out=10):
        try:
            element = WebDriverWait(driver,time_out).until(
                EC.visibility_of_element_located((by,value)))
            self.logger.info(f'定位到元素\t{by}--{value}')
            return element
        except Exception as e:
            self.logger.warning(f'定位不到元素\t{by}--{value}')
            self.logger.warning('\t' + str(e))
            return None
    #重写click方法
    def click_new(self,driver,by,value):
        try:
            el1 = self.find_element_new(driver,by,value)
            el1.click()
            self.logger.info(f'定位到元素\t{by}--{value}')
        except Exception as e:
            self.logger.warning(f'定位不到元素\t{by}--{value}')
            self.logger.warning('\t' + str(e))

    # 重写send_keys方法
    def send_keys_new(self, driver, by, value, text):
        try:
            el1 = self.find_element_new(driver, by, value)
            el1.clear()
            el1.send_keys(text)
            self.logger.info(f'成功输入内容\t{by}--{value}--{text}')
        except Exception as e:
            self.logger.eror(f'无法输入内容\t{by}--{value}--{text}')
            self.logger.error('\t' + str(e))
if __name__=='__main__':
    from config.comm_driver import CommonDriver
    from base.base_object import Authorize_Object
    dr = CommonDriver().get_driver()
    Base().click_new(dr,AppiumBy.XPATH,'123456')
    Base().click_new(dr,AppiumBy.XPATH,Authorize_Object().privacy_agree_button())
#
# if __name__=='__main__':
#     from config.comm_driver import CommonDriver
#     from base.authorize_object import Authorize_Object
#     dr = CommonDriver().get_driver()
#     Base().find_element_new(dr,AppiumBy.XPATH,'123456')
#     Base().find_element_new(dr,AppiumBy.XPATH,Authorize_Object().privacy_agree_button())